package com.cpan.android.sms;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cpan.android.sms.dialog.SMSFaceDialog;
import com.cpan.android.sms.dialog.SMSPhraseDialog;
import com.cpan.android.sms.util.FaceUtil;
import com.cpan.android.sms.util.SMSServices;

public class SMSNewActivity extends Activity {
	private final String TAG = "CreateSMSActivity";

	private Context mContext;
	private SMSServices mSMSServices;
	private FaceUtil mFaceUtil;
	private EditText txtPhoneNo;
	private EditText txtSmsBody;
	private ImageButton btnFace;
	private ImageButton btnPhrase;
	private ImageButton btnSendSMS;

	private String strPhoneNo;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.sms_read_layout);
		mSMSServices = SMSServices.getInstance(mContext);
		mFaceUtil = FaceUtil.getInstance(mContext);
		strPhoneNo = getIntent().getExtras().getString("msgAdress");

		txtPhoneNo = (EditText) findViewById(R.id.sms_send_to);
		txtSmsBody = (EditText) findViewById(R.id.txt_new_msgs);
		btnFace = (ImageButton) findViewById(R.id.btn_face);
		btnPhrase = (ImageButton) findViewById(R.id.btn_common_word);
		btnSendSMS = (ImageButton) findViewById(R.id.btn_send_sms);

		if (null != strPhoneNo && !"".equals(strPhoneNo)) {
			txtPhoneNo.setText(strPhoneNo);
		}
		btnSendSMS.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				mSMSServices.sendSMS(txtPhoneNo.getText().toString(),
						txtSmsBody.getText().toString());
				backToListActivity();
			}
		});
		btnFace.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(R.id.btn_face);
			}
		});
		btnPhrase.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(R.id.btn_common_word);
			}
		});
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case R.id.btn_face:
			SMSFaceDialog faceDialog = new SMSFaceDialog(mContext);
			faceDialog.OnFacesReturnListener(mOnFacesReturnListener);
			return faceDialog;
		case R.id.btn_common_word:
//			SMSPhraseDialog phraseDialog = new SMSPhraseDialog(mContext);
//			return phraseDialog;
		default:

			break;
		}
		return super.onCreateDialog(id);
	}

	public void backToListActivity() {
		Intent back = new Intent();
		back.setClass(mContext, SMSBoxListActivity.class);
		startActivity(back);
		this.finish();
	}

	public SMSFaceDialog.OnFacesReturnListener mOnFacesReturnListener = new SMSFaceDialog.OnFacesReturnListener() {
		public void setFaces(String faces) {
			mFaceUtil.insertStringToEditText(txtSmsBody, faces);
		}
	};
}
